package imgscala;

import org.apache.commons.io.Charsets;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by donar on 17/12/13.
 */
public class Demo3 {
    public static void main(String[] args) throws Exception {
        testtaskPost();
    }
    public static  void testtaskPost()throws  Exception{
        HttpClient httpclient =  HttpClients.createDefault();
        try {
            //新建一个httpclient Post 请求
            HttpPost httppost = new
                    HttpPost("https://merchant.joom.com/api/images/upload");
           // httppost.setHeader("content-type", "multipart/form-data");
            httppost.setHeader("accept", "*/*");
            httppost.setHeader("accept-encoding", "gzip, deflate, br");
            httppost.setHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8");
            //httppost.setHeader("cookie", "_ym_uid=1505818899430135766; lang=zh; _ga=GA1.2.508766635.1511249002; _ym_isad=2");
            //httppost.setHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
            //由于只是测试使用 这里的路径对应本地文件的物理路径
            File myfile = new File("/Users/donar/Downloads/xxx.png");
            FileBody bin = new FileBody(myfile,ContentType.create("image/png"),"xxxx.png");

            //向MultipartEntity添加必要的数据
            //这里是后台接收文件的接口需要的参数，根据接口文档需要放在http请求的请求头
            HttpEntity reqEntity = MultipartEntityBuilder.create()
                    .addPart("image", bin)//相当于<input type="file" name="media"/>
                    .build();
            httppost.setEntity(reqEntity);
            HttpResponse response = httpclient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
                System.out.println("服务器正常响应.....");
                HttpEntity resEntity = response.getEntity();
                String result = EntityUtils.toString(resEntity,"UTF-8");
                System.out.println(
                        result
                        );
                System.out.println(resEntity.getContent());
                EntityUtils.consume(resEntity);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.getConnectionManager().shutdown();
            } catch (Exception ignore) {
            }
        }
    }
}
